import { Component, OnInit, Input } from '@angular/core';
import { TreeNode } from 'primeng/primeng';
import { ConfigService } from './cfg.service';
import { MdlSnackbarService } from '@angular-mdl/core';

@Component({
    selector: 'parts',
    templateUrl: './parts.component.html',
    styleUrls: ['./parts.component.scss'],
    providers: [ConfigService]
})
export class PartsComponent implements OnInit {
    id: number;
    scrollHeight: string = "450px";
    procTree: TreeNode[] = [];
    parts: any[];
    checkedParts: any[] = [];
    selectedNode: TreeNode;
    cols: any[] = [
        { field: 'partId', header: '编号', style: { 'width': '10%', 'text-align': 'center' } },
        { field: 'partType', header: '类型', style: { 'width': '20%', 'text-align': 'center' } },
        { field: 'partName', header: '名称', style: { 'width': '35%', 'text-align': 'center' } },
        { field: 'unitName', header: '所属机构', style: { 'width': '35%', 'text-align': 'center' } }
    ];
    @Input()
    bizId: number = 0;

    @Input()
    systemUrl: string = '';

    @Input()
    set taskListId(taskListId: number) {
        if (!taskListId || !this.systemUrl) return;
        this.id = taskListId;
        this.configService.listProcByBizId(this.bizId, this.currentProcDefId, this.systemUrl).subscribe(data => {
            this.procTree = [];
            data.forEach(element => {
                let node = { 'label': element['procDefName'], icon: 'fa fa-folder', id: element['procDefId'], rootFlag: true };
                this.procTree.push(node);
            });
            if (null != this.procTree && this.procTree.length > 0) {
                this.selectedNode = this.procTree[0];
                this.nodeSelect();
            }
        });
    }

    get taskListId(): number { return this.id; }

    currentProcDefId: number = 0;
    @Input()
    set procDefId(procDefId: number){
        if(procDefId){
            this.currentProcDefId = procDefId;
            if (!this.id || !this.systemUrl) return;
            this.configService.listProcByBizId(this.bizId, this.currentProcDefId, this.systemUrl).subscribe(data => {
                this.procTree = [];
                data.forEach(element => {
                    let node = { 'label': element['procDefName'], icon: 'fa fa-folder', id: element['procDefId'], rootFlag: true };
                    this.procTree.push(node);
                });
                if (null != this.procTree && this.procTree.length > 0) {
                    this.selectedNode = this.procTree[0];
                    this.nodeSelect();
                }
            });
        }
    }
    constructor(private mdlSnackbarService: MdlSnackbarService,
        private configService: ConfigService) { }

    ngOnInit() {
        this.scrollHeight = $(document).height() - 245 + 'px';
    }
    /**流程选中事件 */
    nodeSelect() {
        this.configService.listParts(this.selectedNode['id'], this.taskListId, this.systemUrl).subscribe(parts => {
            this.checkedParts = [];
            this.parts = parts;
            this.parts.forEach(ele => {
                if (ele['code'] == 1) this.checkedParts.push(ele);
            });
        })
    }
    /** 保存选中项到数据库 */
    saveUpdate() {
        let checkedPartIds = [];
        this.checkedParts.forEach(cPart => checkedPartIds.push(cPart.partId));
        let partIds = checkedPartIds.join(",");
        this.configService.saveUpdateTaskListPart(this.taskListId, +this.selectedNode['id'], partIds, this.systemUrl).subscribe(data => {
            this.mdlSnackbarService.showToast("栏目参与者权限配置成功");
        });
    }
}
